package com.springboot.studentmanagedemo.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.springboot.studentmanagedemo.model.Student;
import com.springboot.studentmanagedemo.service.StudentService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: songhaonan
 * @createDate: 2021/12/6
 * @description: student控制层
 */

@RestController
public class StudentController {

    /*
    构造器注入
     */

    private final StudentService studentService;

    public StudentController(StudentService studentService) {
        this.studentService = studentService;
    }


    /*
    分页查询学生表
     */
    @PostMapping(value = "/queryStudent")
    public Map selectStudent(HttpSession session, @RequestParam(required = false, defaultValue = "1") int page,@RequestParam(required = false) int limit) {

        System.out.println("page:"+page);
        System.out.println("limit:"+limit);
        //定义页数和条数
        PageHelper.startPage(page, limit);
        List<Student> list = studentService.query();
        //将数据传给rolePageInfo
        PageInfo<Student> rolePageInfo = new PageInfo<>(list);
        //转换格式
        Map map = new HashMap<String, Object>();
        //状态码如果成功返回0
        map.put("code", "0");
        //错误代码
        map.put("msg", "");
        //总数量（分页）
        map.put("count", rolePageInfo.getTotal());
        //返回实体对象集合
        map.put("data", rolePageInfo.getList());
        return map;
    }

    /*
    条件查询
     */
    @PostMapping(value = "/queryStudentBy")
    public Map queryStudentW(HttpSession session, @RequestParam(required = false, defaultValue = "1") int page
                                            ,@RequestParam(required = false) int limit,Integer studNo,String school) {

        System.out.println("page:"+page);
        System.out.println("limit:"+limit);
        System.out.println("参数1"+studNo);
        System.out.println("参数2"+school);

        //定义页数和条数
        PageHelper.startPage(page, limit);
        List<Student> list = studentService.selectByStudNoAndSchool(studNo,school);
        //将数据传给rolePageInfo
        PageInfo<Student> rolePageInfo = new PageInfo<>(list);
        //转换格式
        Map map = new HashMap<String, Object>();
        //状态码如果成功返回0
        map.put("code", "0");
        //错误代码
        map.put("msg", "");
        //总数量（分页）
        map.put("count", rolePageInfo.getTotal());
        //返回实体对象集合
        map.put("data", rolePageInfo.getList());
        return map;
    }

    /*
    修改学生信息 restful风格
    @ModelAttribute运用在参数上，会将客户端传递过来的参数按名称注入到指定对象中,
    并且会将这个对象自动加入ModelMap中，便于View层使用
    @ModelAttribute("Student")
     */
    @PutMapping(value = "/editStudent")
    @Transactional
    public String updateStudent(Student student) {
        System.out.println(student.toString());
        int i = studentService.updateStudent(student);
        String mes = "";
        if(i==1){
            mes = "成功修改学生信息";
        }
        mes = "修改失败?";
        return mes;
    }

    /*
    添加学生接口
    @Param Student
    @return String
     */
    @PostMapping("/addStudent")
    @Transactional
    public String insertStudent(Student student) {
        System.out.println(student.toString());
        int i = studentService.insertStudent(student);
        String mes = "";
        if(i==1){
            mes = "成功添加学生";
        }
        mes = "添加失败?";
        return mes;
    }

    /*
    删除学生接口
    @Param Student
    @return String
     */
    @DeleteMapping("/deleteStudent")
    @Transactional
    public String deleteStudent(Student student) {
        System.out.println(student.toString());
        int i = studentService.deleteStudent(student.getStudNo());
        String mes = "";
        if(i==1){
            mes = "删除学号为:"+student.getStudNo()+"的学生成功！";
        }
        mes = "删除失败";
        return mes;
    }

    /*
    批量删除接口
    @Param List<Integer>
    @return String
     */
    @DeleteMapping("/deleteAllStu")
    @ResponseBody
    public String deleteAllStudent(String ids) {
        System.out.println(ids);
        List<String> delList = new ArrayList<>();
        String[] strs = ids.split(",");
        for (String str : strs) {
            delList.add(str);
        }
        String mes = "";
        int i = studentService.deleteAllStudent(delList);
        if (i>0){
            mes = "成功删除"+i+"个学生";
        }else {
            mes = "删除失败";
        }
        return mes;
    }

}
